<template>
  <Card :dis-hover="true">
    <base-table :data="data" :columns="columns" @add="addHandler" 
    @update="updateHandler" @check="checkHandler" @approve="approveHandler"
    @delete="deleteHandler" ck op></base-table>
  </Card>
</template>

<script>
import moment from 'moment';
import BaseTable from '@c/table/table';
import { newApiParams } from '@/common/utils';
export default {
  name: 'Disable',
  components: {
    BaseTable
  },
  data () {
    return {
      levelArray: [
        {
          key: '全国',
          value: 0
        },
        {
          key: '省级',
          value: 1
        },
        {
          key: '市级',
          value: 2
        }
      ],
      sourceArray: [
        {
          key: '科研',
          value: 0
        },
        {
          key: '其它',
          value: 1
        }
      ],
      chacterArray: [
        {
          key: '软件工程',
          value: 0
        },
        {
          key: '其它',
          value: 1
        }
      ],
      columns: [
        { title: ' ', type: 'index', width: 50 },
        { title: '标题', key: 'Name', tooltip: true },
        { title: '填报人', key: 'Creater' },
        { title: '导师', key: 'TutorName' },
        {
          title: '项目级别',
          render: (h, { row }) => {
            let str = '';
            for (let k = 0; k < this.levelArray.length; k++) {
              if (row['Level'] === this.levelArray[k].value) {
                str = this.levelArray[k].key;
                break;
              }
            }
            return h('span', {}, str);
          }
        },
        {
          title: '项目来源',
          render: (h, { row }) => {
            let str = '';
            for (let k = 0; k < this.sourceArray.length; k++) {
              if (row['Source'] === this.sourceArray[k].value) {
                str = this.sourceArray[k].key;
                break;
              }
            }
            return h('span', {}, str);
          }
        },
        {
          title: '项目性质',
          render: (h, { row }) => {
            let str = '';
            for (let k = 0; k < this.chacterArray.length; k++) {
              if (row['Character'] === this.chacterArray[k].value) {
                str = this.chacterArray[k].key;
                break;
              }
            }
            return h('span', {}, str);
          }
        },
        { 
          title: '截止时间',
          width: 200,
          render: (h, { row }) => {
            let start = moment(row['Start']).isValid() ? moment(row['Start']).format('YYYY/MM/DD') : 'YYYY/MM/DD';
            let end = moment(row['End']).isValid() ? moment(row['End']).format('YYYY/MM/DD') : 'YYYY/MM/DD';
            return h('span', {}, `${start} 至 ${end}`);
          }
        }
      ],
      data: [],
      disableRepeatClick: false // 防止表单重复提交
    };
  },
  methods: {
    updateHandler (rows) {
      // console.log(rows);
      if (Array.isArray(rows) && rows.length === 1) {
        this.$router.push({
          name: '项目详情',
          params: {
            addMode: false,
            editable: true,
            project: rows[0]
          }
        });
      } else {
        this.$Message.warning('请选择一条数据操作！');
      }
    },
    checkHandler (rows) {
      if (Array.isArray(rows) && rows.length === 1) {
        this.$router.push({ name: '项目审核', params: { project: rows[0], editorable: false, type: 3 } });
      } else {
        this.$Message.warning('请选择一条数据操作！');
      }
    },
    approveHandler (rows) {
      if (Array.isArray(rows) && rows.length === 1) {
        this.$api.ProjectApprove(rows[0]).then((res) => {
          console.log(res);
        }).catch((err) => { 
          if (err) {
            this.$Message.error(err, 4);
          }
        });
      } else {
        this.$Message.warning('请选择一条数据操作！');
      }
    },
    deleteHandler (rows) {
      // DeleteImsProjectList
      if (Array.isArray(rows) && rows.length > 0) {
        this.$Modal.confirm({
          title: '操作确认',
          content: '删除后可能会影响其它功能使用，确认删除选中的数据吗？',
          onOk: () => {
            this.$api.DeleteImsProjectList(rows).then((res) => {
              this.$Message.success('操作成功！');
              this.getProject();
            }).catch((err) => {
              if (err) {
                this.$Message.error(err);
              }
            });
          }
        });
      } else {
        this.$Message.warning('请选择至少一条数据操作！');
      }
    },
    addHandler () {
      // console.log('add');
      this.$router.push({
        name: '项目详情',
        params: {
          addMode: true,
          editable: true
        }
      });
    },

    getProject () {
      let params = newApiParams();
      params.query.type = '3';
      params.query.result = '0';
      params.query.status = '3';

      this.$api.QueryProjectFLow(params).then((res) => {
        if (Array.isArray(res.Data) && res.Data.length > 0) {
          this.data = res.Data;
        }
      }).catch((err) => {
        if (err) {
          this.$Message.error(err, 4);
        }
      });
    }
  },
  mounted () {
    this.getProject();
  }
};
</script>

<style scoped>

</style>